/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.network.tcp;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;


import org.latestbit.smartobjects.IDestroyable;
import org.latestbit.smartobjects.network.IHandshakeManagerFactory;
import org.latestbit.smartobjects.network.IPacketFormerFactory;
import org.latestbit.smartobjects.utils.*;

public class TCPSharedResources implements IDestroyable {
	private final String DEFAULT_GROUP_NAME="DEFAULT";
	private final int DEFAULT_INIT_THREADS_CNT = 1;
	private final int DEFAULT_MAX_THREADS_CNT = 5;
	private static final String TCP_SHARED_THREAD_POOL_ID = "TCP_SHARED_THREAD_POOL";
	
	private Map<String, ThreadPool> threadPools = new HashMap<String, ThreadPool>();
	
	private Map<String, SocketMultiplexer> multiplexers = new HashMap<String, SocketMultiplexer>();
	private int referenceCounter = 1;
	private IHandshakeManagerFactory handshakeManagerFactory;
	private IPacketFormerFactory packetFormerFactory;
	private Timer handshakeTimer = new Timer( "HandshakeTimer["+this+"]", true );
	
	public TCPSharedResources() {
	}
	
	public IThreadPool getThreadPool() {
		return getThreadPool(DEFAULT_GROUP_NAME);
	}
	
	public synchronized IThreadPool getThreadPool(String groupName) {
		ThreadPool result = threadPools.get(groupName);
		if(result==null) {
			result = new ThreadPool ( DEFAULT_INIT_THREADS_CNT, DEFAULT_MAX_THREADS_CNT, TCP_SHARED_THREAD_POOL_ID );
			threadPools.put(groupName, result);
		}
		return result;
	}
	
	public SocketMultiplexer getSocketMultiplexer() throws IOException {
		return getSocketMultiplexer(DEFAULT_GROUP_NAME);
	}
	
	public synchronized SocketMultiplexer getSocketMultiplexer(String groupName) throws IOException {
		SocketMultiplexer result = multiplexers.get(groupName);
		if(result==null || (result!=null && result.isFull())) {			
			result = new SocketMultiplexer ( groupName );
			multiplexers.put(groupName, result);
		}
		return result;
	}
	
	public IPacketFormerFactory getPacketFormerFactory() {
		return this.packetFormerFactory;
	}

	public void setPacketFormerFactory(IPacketFormerFactory factory) {
		this.packetFormerFactory = factory;
	}
	
	public IHandshakeManagerFactory getHandshakeManagerFactory() {
		return handshakeManagerFactory;
	}

	public void setHandshakeManagerFactory(IHandshakeManagerFactory factory) {
		handshakeManagerFactory = factory;
		
	}
	
	
	public void setHandshakeTimer(Timer timer) {
		this.handshakeTimer = timer;
	}

	public Timer getHandshakeTimer() {
		return handshakeTimer;
	}

	@Override
	public void destroy() {
		for ( SocketMultiplexer multiplexer : multiplexers.values() ) {
			multiplexer.destroy();
		}
		
		multiplexers.clear();
		
		for ( ThreadPool threadPool : threadPools.values() ) {
			threadPool.destroy();
		}
		
		threadPools.clear();
		
		if(this.getHandshakeTimer()!=null) {
			this.getHandshakeTimer().cancel();
			this.setHandshakeTimer(null);
		}		
	}
	
	public void finalize() {
		destroy();
	}
	
	public synchronized void addRef() {
		this.referenceCounter ++;
	}
	
	public synchronized void release() {
		if(--this.referenceCounter == 0) {
			destroy();
		}
	}
}
